package com.example.dryyzx.dao;

import com.example.dryyzx.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface UserMapper {
    @Delete({
        "delete from user",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into user (id, name, ",
        "sex, age, phone, ",
        "job, salary, employee_number, ",
        "remarks, entry_time, ",
        "is_leaving, leaving_time)",
        "values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, ",
        "#{sex,jdbcType=CHAR}, #{age,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR}, ",
        "#{job,jdbcType=CHAR}, #{salary,jdbcType=INTEGER}, #{employeeNumber,jdbcType=VARCHAR}, ",
        "#{remarks,jdbcType=VARCHAR}, #{entryTime,jdbcType=TIMESTAMP}, ",
        "#{isLeaving,jdbcType=BIT}, #{leavingTime,jdbcType=TIMESTAMP})"
    })
    int insert(User record);

    @InsertProvider(type=UserSqlProvider.class, method="insertSelective")
    int insertSelective(User record);

    @Select({
        "select",
        "id, name, sex, age, phone, job, salary, employee_number, remarks, entry_time, ",
        "is_leaving, leaving_time",
        "from user",
        "where id = #{id,jdbcType=INTEGER} and is_leaving = 0"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
        @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="job", property="job", jdbcType=JdbcType.CHAR),
        @Result(column="salary", property="salary", jdbcType=JdbcType.INTEGER),
        @Result(column="employee_number", property="employeeNumber", jdbcType=JdbcType.VARCHAR),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="entry_time", property="entryTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="is_leaving", property="isLeaving", jdbcType=JdbcType.BIT),
        @Result(column="leaving_time", property="leavingTime", jdbcType=JdbcType.TIMESTAMP)
    })
    User selectByPrimaryKey(Integer id);

    @Select({
            "select",
            "id",
            "from user",
            "order by id DESC LIMIT 1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true)
    })
    int lastUserId();

    @Select({
            "select",
            "count(*)",
            "from user",
            "where is_leaving = 0"
    })
    int countUser();

    @Select({
            "select",
            "count(*)",
            "from user",
            "where name like concat('%',#{name,jdbcType=VARCHAR},'%') and is_leaving = 0"
    })
    int countUserByName(String name);

    @Select({
            "select",
            "id, name, sex, age, phone, job, salary, employee_number, remarks, entry_time, ",
            "is_leaving, leaving_time",
            "from user",
            "where is_leaving=0",
            "limit #{arg0,jdbcType=INTEGER} offset #{arg1,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
            @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="job", property="job", jdbcType=JdbcType.CHAR),
            @Result(column="salary", property="salary", jdbcType=JdbcType.INTEGER),
            @Result(column="employee_number", property="employeeNumber", jdbcType=JdbcType.VARCHAR),
            @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
            @Result(column="entry_time", property="entryTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="is_leaving", property="isLeaving", jdbcType=JdbcType.BIT),
            @Result(column="leaving_time", property="leavingTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<User> selectByLimitAndOffset(Integer arg0,Integer arg1);

    @Select({
            "select",
            "id, name, sex, age, phone, job, salary, employee_number, remarks, entry_time, ",
            "is_leaving, leaving_time",
            "from user",
            "where name like concat('%',#{arg2,jdbcType=VARCHAR},'%') and is_leaving=0",
            "limit #{arg0,jdbcType=INTEGER} offset #{arg1,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
            @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="job", property="job", jdbcType=JdbcType.CHAR),
            @Result(column="salary", property="salary", jdbcType=JdbcType.INTEGER),
            @Result(column="employee_number", property="employeeNumber", jdbcType=JdbcType.VARCHAR),
            @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
            @Result(column="entry_time", property="entryTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="is_leaving", property="isLeaving", jdbcType=JdbcType.BIT),
            @Result(column="leaving_time", property="leavingTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<User> selectByLimitAndOffsetName(Integer arg0,Integer arg1,String arg2);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    @Update({
        "update user",
        "set name = #{name,jdbcType=VARCHAR},",
          "sex = #{sex,jdbcType=CHAR},",
          "age = #{age,jdbcType=INTEGER},",
          "phone = #{phone,jdbcType=VARCHAR},",
          "job = #{job,jdbcType=CHAR},",
          "salary = #{salary,jdbcType=INTEGER},",
          "employee_number = #{employeeNumber,jdbcType=VARCHAR},",
          "remarks = #{remarks,jdbcType=VARCHAR},",
          "entry_time = #{entryTime,jdbcType=TIMESTAMP},",
          "is_leaving = #{isLeaving,jdbcType=BIT},",
          "leaving_time = #{leavingTime,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(User record);

    @Update({
            "update user",
            "set leaving_time = null",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int setLeavingTimeNullByPrimaryKey(Integer id);
}